/**
 * Created by feifan on 2016/6/4.
 */
const low = require('lowdb')(__dirname + '/../data/db.json');
const db = low;
const _=require('lodash');

db.select = (table, query, cb)=> {
    cb(low.get(table).filter(query).value());
};
db.findOne = (table, query, cb)=> {
    cb(low.get(table).find(query).value());
};
db.insert = (table, value, cb)=> {
    low.get(table).push(value).value();
    cb&&cb();
};
db.update = (table, query, value, cb)=> {
    low.get(table).filter(query).assign(value).save();
    cb();
};

db.del = (table, query, cb)=> {
    low.get(table).remove(query);
    cb();
};


//var log= console.log.bind(console);
//var fs=require('fs')
//var t =db.get('articles').getById(1).value()
//log(t)
//db.findOne('articles', {id:1}, (data)=> {
//    log(data)
//    if(data){
//        let result=data;
//        fs.readFile('./articles/'+data.id+'.html','utf8', (err, html) => {
//            if (err){
//                log(err);
//                result.content="error";
//            }else{
//                result.content=html;
//            }
//
//            log(result);
//        });
//    }
//});

//var data =db.get('articles').sortBy('id').last().value();
//console.log(data)
//db.findOne('user', {user: "123",pwd:'321'},(data)=>{
//    console.log(data)
//})
//var d=db.select('articles',(item)=>{
//    return item.category&&item.category.indexOf('category1')>-1;
//},(data)=>{
//    console.log(data)
//});
//{category: 'category1' }
//var dd = db.get('articles').value();
//var d2=_.filter(dd,(item)=>{
//    //console.log(item.id)
//    return _.filter(item.tag,(e)=>{
//        return ['tag1'].indexOf(e)>-1;
//    }).length>0;
//});
//var d3= _.filter(['tag1','tag2'],(item)=>{
//    return ['tag2'].indexOf(item)>-1;
//})
//console.log(d2);
//db.insert('category',['cate1']);
//db.insert('tags','tag2');
//db.select('nav',()=>true,(data)=>{
//    console.log(data)
//    //res.json(data.nav);
//})
//var mongoskin = require('mongoskin'),
//    config = require('../config.json'),
//    host = config.DB_host,
//    port = config.DB_port,
//    dbName = config.DB_name,
//    userName = config.DB_user,
//    password = config.DB_pwd,
//    pre = config.DB_prefix,
//    str = 'mongodb://' + userName + ':' + password + '@' + host + ':' + port + '/' + dbName,
//    option = {native_parser: true},
//    db = mongoskin.db(str, option);
//db.bind('')
//db.collection('node_setting').findOne({},(err,res)=>{
//    console.log(res)
//})
//db.selectAll('setting',(data)=>{
//    log(data[0]);
//});
//console.log(db)
//db.select = function (table, query) {
//    query = query || {};
//    return db.collection(pre + table).find(query);
//};
//db.insert = function (table, query, callback) {
//    if (!query)return;
//    db.collection(pre + table).insert(query, function (err, res) {
//        if (err)console.log(err);
//        callback(err, res);
//    });
//};
//db.update = function (table, query, obj) {
//    db.collection(pre + table).update(query, {$set: obj}, function (err, res) {
//        if (err)console.log(err);
//    });
//};
//db.delete = function (table, query, callback) {
//    db.collection(pre + table).deleteMany(query, function (err, res) {
//        if (err)console.log(err);
//        callback(err, res);
//    });
//};
//db.selectAll = function (table, query, callback) {
//    db.select(table, query).toArray(function (err, res) {
//        if (err)console.log(err);
//        callback(res);
//    });
//};
module.exports = db;